package com.zhou.util;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
 * 
 * @author  
 * @2012-9-16 下午4:05:34
 */
public final class ZipUtil {

  // 压缩
  public static String gzip(String str) throws IOException {
    return gzip(str,"UTF-8");
  }

    public static String gzip(String str,String charset) throws IOException {
        if (str == null || str.length() == 0) {
            return str;
        }
        GZIPOutputStream gzip = null;
        try {
            ByteArrayOutputStream out  = new ByteArrayOutputStream();
            gzip = new GZIPOutputStream(out);
            gzip.write(str.getBytes());
            return out.toString(charset);
        } catch (IOException e) {
            throw e;
        } finally {
            try {
                if (gzip != null) gzip.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
  
  public static byte[] gzip(byte[] bytes) throws IOException {
      GZIPOutputStream gzip = null;
      try {
          if (bytes == null || bytes.length == 0) {
            return bytes;
          }
          ByteArrayOutputStream out = new ByteArrayOutputStream();
        gzip = new GZIPOutputStream(out);
          gzip.write(bytes);

          return out.toByteArray();
      } catch (IOException e) {
          throw e;
      } finally {
          try {
              if (gzip != null) gzip.close();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  }
  
  

  // 解压
  public static byte[] unGzip(byte[] bytes) throws IOException {
	    if (bytes == null || bytes.length == 0) {
	      return bytes;
	    }
        ByteArrayOutputStream out = null;
        GZIPInputStream gunzip = null;
      try {
          out = new ByteArrayOutputStream();
          ByteArrayInputStream in = new ByteArrayInputStream(bytes);
          gunzip = new GZIPInputStream(in);
          byte[] buffer = new byte[256];
          int n;
          while ((n = gunzip.read(buffer)) >= 0) {
            out.write(buffer, 0, n);
          }
          return out.toByteArray();
      } catch (IOException e) {
         throw e;
      } finally {
          try {
              if (out != null) out.close();
              if (gunzip != null) gunzip.close();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  }

    public static String unGzip(String str) throws IOException {
        return unGzip(str,"UTF-8");
    }
  // 解压
  public static String unGzip(String str,String charset) throws IOException {
    if (str == null || str.length() == 0) {
      return str;
    }
      ByteArrayOutputStream out = null;
      GZIPInputStream gunzip = null;
      try {
           out = new ByteArrayOutputStream();
          ByteArrayInputStream in = new ByteArrayInputStream(str
              .getBytes(charset));
          gunzip = new GZIPInputStream(in);
          byte[] buffer = new byte[256];
          int n;
          while ((n = gunzip.read(buffer)) >= 0) {
            out.write(buffer, 0, n);
          }
          return out.toString(charset);
      } catch (IOException e) {
//          e.printStackTrace();
          throw e;
      } finally {
          try {
              if (out != null) out.close();
              if (gunzip != null) gunzip.close();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  }

}
